1 ========================================================================
2 VBASPNETAJAXConsumeExternalWebService Overview
3 ========================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 The project illustrates how to consume an external Web Service from a
11 /////////////////////////////////////////////////////////////////////////////
14 Please follow this demonstration steps below.
16 Step 1: Open the VBASPNETAJAXConsumeExternalWebService.sln.
18 Step 2: Expand the ExternalWebSite and right-click the ExternalWebService.asmx
19 and click "View in Browser". This step is very important,
20 it impersonates to open an external web service.
22 Step 3: Expand the TestWebSite and right-click the default.aspx and click
25 Step 4: You will see a black panel and a Button. Click on the button. You will
26 see "Please wait a moment..." in the Panel, after about one second or
27 less time, you will see the datetime returned from the server.
31 /////////////////////////////////////////////////////////////////////////////
34 Step 1. Create an VB.NET "ASP.NET Empty Web Site" in Visual Studio 2010 or
35 Visual Web Developer 2010. Change the last folder name to ExternalWebSite.
37 Step 2. Add a new "Web Service" item. We call it ExternalWebService.asmx.
39 Step 3. Open the ExternalWebService.asmx.vb in App_Code.
41 Step 4. Un-comment this line above the class name.
43 <System.Web.Script.Services.ScriptService()>
46 Step 5. Write a new web method like below and save the file.
49 Public Function GetServerTime() As DateTime
54 Step 6. Open the ExternalWebService.asmx to view the page in the browser.
55 Copy the URL address in the navigation bar.
57 Step 7. Add a new C# "ASP.NET Empty Web Site". Change the last folder name
60 Step 8. Click on the new website in the Solution Explorer and look at the menu
61 bar. Click the WebSite -> Add Web Reference...
63 Step 9. Paste the URL address which we get in the step 6 into the "URL" textbox.
64 Change the "Web Reference Name" to "ExternalWebService". Click
67 Step 10. Create a new "Web Service" item. Change the name to BridgeWebService.asmx
69 Step 11. Open the BridgeWebService.asmx.vb in the App_Code. Do the same thing in
72 Step 12. Write the code below to call the external web service and save the file.
75 Public Function GetServerTime() As DateTime
76 ' Get an instance of the external web service
77 Dim ews As ExternalWebService.ExternalWebService = New ExternalWebService.ExternalWebService()
78 ' Return the result from the web service method.
79 Return ews.GetServerTime()
83 Step 13. Create a new "Web Form" item. Chage the name to Default.aspx.
85 Step 14. Add a ScriptManager control into the page. And a service reference
86 which path is the local bridge web service: BridgeWebService.asmx.
88 Step 15. Create a DIV for showing the result and a Button to call the service.
91 style="width: 100%; height: 100px; background-color: Black; color: White">
94 value="Get Server Time From External Web Service"
95 onclick="GetServerDateTime()" />
98 Step 16. Create the Javascript functions to call the web service.
100 <script type="text/javascript">
101 // This function is used to call the service by Ajax Extension.
102 function GetServerDateTime() {
103 $get("Result").innerHTML = "Please wait a moment...";
104 BridgeWebService.GetServerTime(onSuccess, onFailed);
106 // This function will be executed when get a response
108 function onSuccess(result) {
109 $get("Result").innerHTML = "Server DateTime is : " + result.toLocaleString();
112 // This function will be executed when get an exception
114 function onFailed(args) {
115 alert("Server Return Error:\n" +
116 "Message:" + args.get_message() + "\n" +
117 "Status Code:" + args.get_statusCode() + "\n" +
118 "Exception Type:" + args.get_exceptionType());
122 When we add the ServiceReference of the ScriptManager, we will see
125 Step 17. Test the Default.aspx and we will get the datetime from the server.
128 If we want to use an AJAX-enabled WCF service from a different domain with
129 Service Reference of the ScriptManager, the steps are same as we talked above.
130 Just create a local AJAX-enabled WCF service to make a bridge to consume the
132 /////////////////////////////////////////////////////////////////////////////
135 MSDN: ServiceReference Class
136 http://msdn.microsoft.com/en-us/library/system.web.ui.servicereference.aspx
138 MSDN: ASP.NET Web Services
139 http://msdn.microsoft.com/en-us/library/t745kdsh.aspx
141 /////////////////////////////////////////////////////////////////////////////